package cn.cboard.component.tree;

import junit.framework.TestCase;
/*
 *   | 1000
 *   |-- 3000
 *   |-- |--4000
 *   |-- |--6000 
 *   |-- |-- |-- 6100
 *   |-- |-- |-- 6200 
 *   |-- |--6700
 *   |-- |--7000
 *   |-- |-- |--8000
 *   |-- |-- |-- |--9000
 *   |-- |-- |-- |--10000   
 *   |-- 5000 
 */

public class LongTreeTest extends TestCase{
	private LongTree tree;
	
	public void setUp() throws Exception{
		super.setUp();
		
		tree = new LongTree(1000,12);
		
        tree.addChild(1000, 3000);
        tree.addChild(1000, 5000);
        tree.addChild(3000, 4000);
        tree.addChild(3000, 6000);
        tree.addChild(6000, 6100);
        tree.addChild(6000, 6200);
        tree.addChild(3000, 6700);
        tree.addChild(3000, 7000);
        tree.addChild(7000, 8000);
        tree.addChild(8000, 9000);
        tree.addChild(8000, 10000);
	}
	
	public void testGetParent() {
        long result = tree.getParent(5000);
        assertEquals(result, 1000);
    }

    public void testGetChild() {
        long result = tree.getChild(3000, 1);
        assertEquals(result, 6000);
    }

    public void testGetChildCount() {
        int result = tree.getChildCount(3000);        
        System.out.println("result=" + result);
        assertEquals(result, 4);
    }

    public void testGetChildren() {
        long[] childern = tree.getChildren(6000);
        for(int i=0; i<childern.length; i ++){
            System.out.println(" child=" + childern[i]);
        }
    }

    public void testGetIndexOfChild() {
        int index = tree.getIndexOfChild(6000, 6200);
        System.out.println("index=" + index);
        assertEquals(index, 1);
    }

    public void testGetDepth() {
        int index = tree.getDepth(7000);
        System.out.println("index=" + index);
        assertEquals(index, 2);
    }

    public void testGetRecursiveChildren() {
    	long[] result = tree.getRecursiveChildren(6000);
    	/*for(int i=0; i<result.length; i++){
    		System.out.println(result[i]);
    	}*/
    	assertEquals(result.length, 2);
    }

    public void testIsLeaf() {
        boolean result = tree.isLeaf(3000);
        assertEquals(result, false);
        
        result = tree.isLeaf(4000);
        assertEquals(result, true);
        
        result = tree.isLeaf(8000);
        assertEquals(result, false);
        
        result = tree.isLeaf(9000);
        assertEquals(result, true);
    }
    
    public void testKeys(){
    	long[] keys = tree.keys();
    	for(int i=0; i<keys.length; i++){
    		System.out.println(keys[i]);
    	}
    	assertEquals(keys.length, 3);
    }
}
